package com.qingke.easyjava.jdbc.app;

import com.qingke.easyjava.jdbc.command.Command;
import com.qingke.easyjava.jdbc.command.CommandFactory;
import com.qingke.easyjava.jdbc.command.CommandFactory.CommandCode;
import com.qingke.easyjava.jdbc.pojo.Player;
import com.qingke.easyjava.jdbc.util.QingkeConsole;

public class BeSuperApplication {
    
    // singleton 独自
	//System.out.println(connnection.getClass().getName());
    private static BeSuperApplication instance = null;

    private BeSuperApplication() {
    }

    public static synchronized BeSuperApplication getInstance() {

        if (instance == null) {
            instance = new BeSuperApplication();
        }

        return instance;
    }

    private Player playerProfile;

    public static void main(String[] args) {

        // start
        QingkeConsole.println("You can use the \"HELP\" for command usage. Have fun!");

        while (true) {
            String cmd = QingkeConsole.askUserInput("cmd");
           
            CommandCode cmdCode = null;
            try {
                cmdCode = CommandCode.valueOf(cmd.toUpperCase());//toUpperCase 全部变成大写通过valueOf，返回带指定名称的指定枚举类型的枚举常量。名称必须与在此类型中声明枚举常量所用的标识符完全匹配
                
            } catch (Exception e) {
                QingkeConsole.println("你输入了一个不合法的命令，请使用 Help 查看帮助");
                continue;										//continue跳过本次循环，在while中即为重头来过。达到输入循环的目的。
            }
            // .getFactory( Player player )： 需要传入一个 player内容，playerProfile没有赋值为空。factory = SystemCommandFactory; 调用SystemCommandFactory.buildCommand方法。
            CommandFactory factory = CommandFactory.getFactory(BeSuperApplication.getInstance().getPlayerProfile());
            Command command = factory.buildCommand(cmdCode);//类型为Command 因为所有的 XXXcommand文件都继承Command，
           
            if (command == null) {
                QingkeConsole.println("请先登陆！");
                continue;
            }
            command.execute();
        }
    }

    public Player getPlayerProfile() {
        
        return playerProfile;
    }

    public void setPlayerProfile(Player player) {
        this.playerProfile = player;
    }
}
